home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / QuickTime VR / MacOS / QuickDraw™ 3D 1.0.6F4 SDK / Development / RAVE DDK 1.0.6 GM for MacOS / Projects / Test Code / TinselWindow.c < prev   
Encoding:
C/C++ Source or Header  |  1996-04-30  |  10.7 KB  |  471 lines  |  [TEXT/MPCC]

  1. /******************************************************************************
  2.  **                                                                             **
  3.  **     Module:        TinselWindow.c                                             **
  4.  **                                                                          **
  5.  **     Purpose:     Window/GDevice stuff for Tinsel Town test app             **
  6.  **                                                                          **
  7.  **     Author:        Mike W. Kelley                                             **
  8.  **                                                                             **
  9.  **                    2/3/95    Revised for 0.9 SDK release                         **
  10.  **                                                                          **
  11.  **     Copyright (C) 1994-95 Apple Computer, Inc.  All rights reserved.     **
  12.  **                                                                          **
  13.  *****************************************************************************/
  14.  
  15.  
  16. #include <stdlib.h>
  17. #include <math.h>
  18. #include <string.h>
  19. #include <stdio.h>
  20.  
  21. /* Macintosh */
  22. #include <Types.h>
  23. #include <Resources.h>
  24. #include <QuickDraw.h>
  25. #include <Fonts.h>
  26. #include <Events.h>
  27. #include <Windows.h>
  28. #include <Menus.h>
  29. #include <TextEdit.h>
  30. #include <Dialogs.h>
  31. #include <Desk.h>
  32. #include <ToolUtils.h>
  33. #include <Memory.h>
  34. #include <SegLoad.h>
  35. #include <Files.h>
  36. #include <OSUtils.h>
  37. #include <OSEvents.h>
  38. #include <DiskInit.h>
  39. #include <Packages.h>
  40.  
  41. /* Project */
  42. #include "Drive3D.h"
  43. #include "TinselTest.h"
  44.  
  45. /*******************************************************************************************
  46.  *
  47.  * Static variables used by this file's functions only.
  48.  *
  49.  ******************************************************************************************/
  50.  
  51. static    GDHandle        gGDevice;
  52. static    WindowPtr        gTinselWindow [kTinselWindows];
  53. static    RgnHandle        gClipRgn [kTinselWindows];
  54.  
  55. /*******************************************************************************************
  56.  *
  57.  * Utility functions
  58.  *
  59.  ******************************************************************************************/
  60.  
  61. void InitToolbox (void)
  62. {
  63.     InitGraf((Ptr) &qd.thePort);
  64.     InitFonts();
  65.     InitWindows();
  66.     InitMenus();
  67.     FlushEvents(everyEvent,0);
  68.     TEInit();
  69.     InitDialogs(0L);
  70.     InitCursor();
  71. }
  72.  
  73. /************************************************************************************************
  74.  *
  75.  * Error handling stuff.
  76.  *
  77.  ***********************************************************************************************/
  78.  
  79. void TinselError (
  80.     const char        *message)
  81. {
  82.     char            ErrorStr[256];
  83.  
  84.     sprintf (&ErrorStr[1], "TinselError: %s\n", message);
  85.     ErrorStr[0] = strlen (&ErrorStr[1]) - 1;
  86.     DebugStr ( (ConstStr255Param) ErrorStr);
  87. }
  88.  
  89. /************************************************************************************************
  90.  *
  91.  * Choose a drawing engine for 'windowNumber'. This returns the engine name.
  92.  *
  93.  ***********************************************************************************************/
  94.  
  95. static TQAEngine *ChooseEngine (
  96.     TQADevice        *device,
  97.     long            windowNumber,
  98.     char            engineName[64])        /* Out */
  99. {
  100.     TQAEngine        *engine;
  101.     long            i;
  102.     Boolean            alreadyUsed;
  103.     TQAError        gestaltStatus;
  104.     long            engineNameLength;
  105.     
  106.     for (engine = QADeviceGetFirstEngine (device);
  107.             engine;
  108.             engine = QADeviceGetNextEngine (device, engine))
  109.     {
  110.         /*
  111.          * Search to see if this engine is already being used.
  112.          */
  113.         
  114.         for (i = 0, alreadyUsed = false; i < windowNumber; ++i)
  115.         {
  116.             if (gEngine [i] == engine)
  117.             {
  118.                 alreadyUsed = true;
  119.             }
  120.         }
  121.         
  122.         if ( ! alreadyUsed)
  123.         {
  124.             /*
  125.              * Use this engine.
  126.              */
  127.             
  128.             break;
  129.         }
  130.     }
  131.     
  132.     if (engine)
  133.     {
  134.         /*
  135.          * A unique drawing engine was found. Do a gestalt call to get the
  136.          * engine name.
  137.          */
  138.         
  139.         gestaltStatus = QAEngineGestalt (engine, kQAGestalt_ASCIINameLength, &engineNameLength);
  140.         if ((gestaltStatus == kQANoErr) && (engineNameLength < 63))
  141.         {
  142.             gestaltStatus = QAEngineGestalt (engine, kQAGestalt_ASCIIName, &engineName[1]);
  143.         }
  144.         
  145.         if (gestaltStatus != kQANoErr)
  146.         {
  147.             /*
  148.              * Gestalt call failed!
  149.              */
  150.             
  151.             sprintf (&engineName[1], "<Gestalt Error>");
  152.         }
  153.     }
  154.     else
  155.     {
  156.         /*
  157.          * No unused engine was found. Assign a NULL engine pointer, and
  158.          * set a dummy engine name.
  159.          */
  160.         
  161.         sprintf (&engineName[1], "<No Drawing Engine>");
  162.     }
  163.     engineName[0] = strlen (&engineName[1]);
  164.     
  165.     return (engine);
  166. }
  167.  
  168. /*******************************************************************************************
  169.  *
  170.  * Create test windows on the deepest GDevice.
  171.  *
  172.  ******************************************************************************************/
  173.  
  174. Boolean CreateTestWindows (
  175.     long            maxWidth,
  176.     long            maxHeight)
  177. {
  178.     GDHandle        colourGDevice, deepestGDevice;
  179.     long            deviceDepth, maxDepth;
  180.     Rect            deviceRect;
  181.     Rect            windowRect;
  182.     long            deviceWidth, deviceHeight;
  183.     long            windowWidth, windowHeight;
  184.     TQADevice        device;
  185.     char            engineName[64];
  186.  
  187.     /*
  188.      * Find the deepest color display.
  189.      */
  190.     
  191.     maxDepth = 0;
  192.     
  193.     for (colourGDevice = GetDeviceList(), deepestGDevice = NULL;
  194.             colourGDevice;
  195.             colourGDevice = GetNextDevice (colourGDevice))
  196.     {
  197.         deviceDepth = (*(*colourGDevice)->gdPMap)->pixelSize;
  198.         
  199.         if (deviceDepth > maxDepth)
  200.         {
  201.             maxDepth = deviceDepth;
  202.             deepestGDevice = colourGDevice;
  203.         }
  204.     }
  205.     gGDevice = deepestGDevice;
  206.     
  207.     /*
  208.      * Open two side-by-side windows which almost fill the device, or to
  209.      * maxWidth, maxHeight.
  210.      */
  211.  
  212.     deviceRect = (*deepestGDevice)->gdRect;
  213.     
  214.     deviceWidth = (deviceRect.right - deviceRect.left) - 2 * kDeviceWindowBorder;
  215.     deviceHeight = (deviceRect.bottom - deviceRect.top) - 2 * kDeviceWindowBorder;
  216.     
  217.     windowWidth = (deviceWidth / 2) - 2 * kDeviceWindowBorder;
  218.     windowHeight = deviceHeight - 2 * kDeviceWindowBorder;
  219.     
  220.     if (windowWidth > maxWidth)
  221.     {
  222.         windowWidth = maxWidth;
  223.     }
  224.     if (windowHeight > maxHeight)
  225.     {
  226.         windowHeight = maxHeight;
  227.     }
  228.     
  229.     gWindowWidth = windowWidth;
  230.     gWindowHeight = windowHeight;
  231.     
  232.     device.deviceType = kQADeviceGDevice;
  233.     device.device.gDevice = deepestGDevice;
  234.  
  235.     /*
  236.      * Choose a drawing engine for the left window, and create the window.
  237.      */
  238.     
  239.     gEngine[0] = ChooseEngine (&device, 0, engineName);
  240.     
  241.     windowRect.top = (deviceRect.top + deviceRect.bottom) / 2 - windowHeight / 2;
  242.     windowRect.bottom = windowRect.top + windowHeight;
  243.  
  244.     windowRect.left = (deviceRect.left * 3 + deviceRect.right) / 4 - windowWidth / 2;
  245.     windowRect.right = windowRect.left + windowWidth;
  246.  
  247.     gTinselWindow[0] = NewCWindow (nil, &windowRect, (void *) engineName, true, zoomDocProc, nil, true, 0);
  248.     
  249.     /*
  250.      * Choose a drawing engine for the right window, and create the window.
  251.      */
  252.     
  253.     gEngine[1] = ChooseEngine (&device, 1, engineName);
  254.     
  255.     windowRect.left = (deviceRect.left + deviceRect.right * 3) / 4 - windowWidth / 2;
  256.     windowRect.right = windowRect.left + windowWidth;
  257.  
  258.     gTinselWindow[1] = NewCWindow (nil, &windowRect, (void *) engineName, true, zoomDocProc, nil, true, 0);
  259.  
  260.     /*
  261.      * If both window creations were successful, return true.
  262.      */
  263.     
  264.     return (gTinselWindow[0] && gTinselWindow[1]);
  265. }
  266.  
  267. /*******************************************************************************************
  268.  *
  269.  * Create test windows on the deepest GDevice.
  270.  *
  271.  ******************************************************************************************/
  272.  
  273. void ClearWindows (void)
  274. {
  275.     long            i;
  276.     WindowPtr        window;
  277.     
  278.     for (i = 0; i < kTinselWindows; ++i)
  279.     {
  280.         GrafPtr        previousPort;
  281.         
  282.         window = gTinselWindow [i];
  283.         
  284.         GetPort (&previousPort);
  285.         SetPort (window);
  286.         EraseRect (&window->portRect);
  287.         SetPort (previousPort);
  288.     }
  289. }
  290.  
  291. /*******************************************************************************************
  292.  *
  293.  * Create test clip region (a round rect) for each test window.
  294.  *
  295.  ******************************************************************************************/
  296.  
  297. Boolean CreateWindowClip (void)
  298. {
  299.     long            i;
  300.     Point             topLeft, botRight;
  301.     WindowPtr        window;
  302.     Rect            windowRect, clipRect;
  303.     
  304.     for (i = 0; i < kTinselWindows; ++i)
  305.     {
  306.         GrafPtr        previousPort;
  307.  
  308.         window = gTinselWindow [i];
  309.         
  310.         /*
  311.          * Get the boundaries of the target window, and compute its size.
  312.          */
  313.         
  314.         topLeft.v = window->portRect.top;
  315.         topLeft.h = window->portRect.left;
  316.         botRight.v = window->portRect.bottom;
  317.         botRight.h = window->portRect.right;
  318.     
  319.         /*
  320.          * Convert window boundaries to global co-ordinates.
  321.          */
  322.         
  323.         GetPort (&previousPort);
  324.         SetPort (window);
  325.         LocalToGlobal (&topLeft);
  326.         LocalToGlobal (&botRight);
  327.         SetPort (previousPort);
  328.     
  329.         /*
  330.          * Create the window bound rectangle in device coordinates.
  331.          */
  332.         
  333.         windowRect.top = topLeft.v - (*gGDevice)->gdRect.top;
  334.         windowRect.left = topLeft.h - (*gGDevice)->gdRect.left;
  335.         windowRect.bottom = botRight.v - (*gGDevice)->gdRect.top;
  336.         windowRect.right = botRight.h - (*gGDevice)->gdRect.left;
  337.         
  338.         /*
  339.          * Build the clip region for this window.
  340.          */
  341.         
  342.         if ( ! (gClipRgn[i] = NewRgn()))
  343.         {
  344.             return (false);
  345.         }
  346.         
  347.         OpenRgn();
  348.         
  349.         clipRect.top = windowRect.top + kWindowClipBoundary;
  350.         clipRect.left = windowRect.left + kWindowClipBoundary;
  351.         clipRect.bottom = windowRect.bottom - kWindowClipBoundary;
  352.         clipRect.right = windowRect.right - kWindowClipBoundary;
  353.         
  354.         FrameRoundRect (&clipRect, kClipRoundRect, kClipRoundRect);
  355.         CloseRgn (gClipRgn[i]);
  356.     }
  357.     
  358.     return (true);
  359. }
  360.  
  361. /*******************************************************************************************
  362.  *
  363.  * Create draw contexts for the test windows.
  364.  *
  365.  ******************************************************************************************/
  366.  
  367. Boolean TestDrawContextNew (
  368.     unsigned long    flags,
  369.     Boolean            do2DClipping)
  370. {
  371.     WindowPtr            window;
  372.     Point                 topLeft, botRight;
  373.     TQARect                windowRect;
  374.     long                i;
  375.     TQADevice            device;
  376.     TQAError            status;
  377.     
  378.     for (i = 0; i < kTinselWindows; ++i)
  379.     {
  380.         if (gEngine [i])
  381.         {
  382.             TQAClip        clip;
  383.             
  384.             window = gTinselWindow [i];
  385.             
  386.             /*
  387.              * Get the boundaries of the target window, and compute its size.
  388.              */
  389.             
  390.             topLeft.v = window->portRect.top;
  391.             topLeft.h = window->portRect.left;
  392.             botRight.v = window->portRect.bottom;
  393.             botRight.h = window->portRect.right;
  394.         
  395.             /*
  396.              * Convert window boundaries to global co-ordinates.
  397.              */
  398.             
  399.             SetPort (window);
  400.             LocalToGlobal (&topLeft);
  401.             LocalToGlobal (&botRight);
  402.         
  403.             /*
  404.              * Create the bound rectangle in device coordinates.
  405.              */
  406.             
  407.             windowRect.top = topLeft.v - (*gGDevice)->gdRect.top;
  408.             windowRect.left = topLeft.h - (*gGDevice)->gdRect.left;
  409.             windowRect.bottom = botRight.v - (*gGDevice)->gdRect.top;
  410.             windowRect.right = botRight.h - (*gGDevice)->gdRect.left;
  411.         
  412.             /*
  413.              * Create the draw context.
  414.              */
  415.             
  416.             device.deviceType = kQADeviceGDevice;
  417.             device.device.gDevice = gGDevice;
  418.     
  419.             clip.clipType = kQAClipRgn;
  420.             clip.clip.clipRgn = gClipRgn[i];
  421.             
  422.             status = QADrawContextNew (&device, &windowRect,
  423.                     do2DClipping ? &clip : NULL, gEngine [i], flags, &gDrawContext [i]);
  424.             
  425.             if (status != kQANoErr)
  426.             {
  427.                 TinselError ("Could not create draw context");
  428.                 return (false);
  429.             }
  430.         }
  431.         else
  432.         {
  433.             /*
  434.              * No drawing engine attached to this window.
  435.              */
  436.             
  437.             gDrawContext [i] = NULL;
  438.         }
  439.     }
  440.     return (true);
  441. }
  442.  
  443. void TestDrawContextDelete (void)
  444. {
  445.     long        i;
  446.     
  447.     for (i = 0; i < kTinselWindows; ++i)
  448.     {
  449.         if (gDrawContext [i])
  450.         {
  451.             QADrawContextDelete (gDrawContext [i]);
  452.             gDrawContext [i] = NULL;
  453.         }
  454.     }
  455. }
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.  
  467.  
  468.  
  469.  
  470.  
  471.